const query = require("../utils/DB.js")
class orderModel {
	static async getOrder(
		nowPage = 1,
		pageSize = 10
	) {
		let start = (nowPage - 1) * pageSize
		let listCount = await query(`select count(*) as num from orders`)
		let list = await query(`select * from orders limit ${start},${pageSize}`)

		if (list.length > 0) {
			for (let i = 0; i < list.length; i++) {
				list[i]['goodsList'] = await query(`select * from ordergoods where oid=${list[i].oid}`)
			}
		}
		return {
			count: listCount[0].num,
			list: list
		}
		// 先获取订单列表
		// let orderList = await query(`select * from order`)
		// // 循环订单列表 获取 每个订单的订单商品列表
		// for (let i = 0; i < orderList.length; i++) {
		// 	orderList[i]['goodsList'] = await query(`select * from ordergoods where oid=${orderList[i].oid}`)
		// }
		// return orderList
	}
	
	// 退款状态 退款原因
	static editOrder(orderstatus,orderremark,oid){
		return query(`update orders set orderstatus=${orderstatus} ,orderremark='${orderremark}' where oid=${oid}`)
	}
	
	// 核销商品 
	static async heXiaoOrder(ogid,status,oid){
		// 根据订单id 查询下 是否所有的商品 都已经核销完毕了
		await query(`update ordergoods set status=${status} where id=${ogid}`)
		let res =  await query(`select * from ordergoods where oid=${oid} and status=1`)
		// 如果长度等于0 说明 这个订单所有的商品都已经核销完毕了
		if(res.length===0){
			await query(`update orders set status=4 where oid=${oid}`)
		}
		return true
	}
	
	
}
module.exports = orderModel